package com.example.happy.controller;

import com.example.happy.entity.Account;
import com.example.happy.mapper.AccountMapper;
import com.example.happy.service.AccountService;
import com.example.happy.vo.DataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Resource
    AccountService accountService;

    @Autowired
    AccountMapper accountMapper;

    @GetMapping("/getAllAccount")
    public Map<String, Object> getAllAccount() {
        Map<String, Object> map = new HashMap<>();
        List<Account> data = this.accountService.list();
        map.put("code", "0000");
        map.put("msg", "成功");
        map.put("data", data);
        return map;
    }

    @PostMapping("/saveAccount")
    public Map<String, Object> save(@RequestBody Account account) {
        account.setCreateTime(new Date());
        Map<String, Object> map = new HashMap<>();
        boolean flag = this.accountService.save(account);
        map.put("code", flag ? "0000" : "0001");
        map.put("msg", flag ? "成功" : "失败");
        return map;
    }

    @PostMapping("/updateAccount")
    public Map<String, Object> updateAccount(Account account) {
        Map<String, Object> map = new HashMap<>();
        boolean flag = this.accountService.updateById(account);
        map.put("code", flag ? "0000" : "0001");
        map.put("msg", flag ? "成功" : "失败");
        return map;
    }

    @DeleteMapping("/deleteAccount/{id}")
    public Map deleteAccount(@PathVariable("id") Integer id){
        Map<String, Object> map = new HashMap<>();
        boolean flag = accountService.removeById(id);

        map.put("code", flag ? "0000" : "0001");
        map.put("msg", flag ? "成功" : "失败");

        return map;
    }

    @GetMapping("/getLineData")
    public Map<String, Object> getLineData(){
        Map<String, Object> map = new HashMap<>();
        //收入数据
        List<DataVo> list1 = accountMapper.getLineData(1);
        //开支数据
        List<DataVo> list2 = accountMapper.getLineData(2);
        map.put("list1", list1);
        map.put("list2", list2);
        return map;
    }

    @GetMapping("/echo")
    public String echo() {
        return "hello-v2.0.9";
    }

}
